package com.not_found404.bilibilimerge;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.Map;

/**
 * Created by chengww on 2017/5/28.
 */

public class CustomDialogFragment extends DialogFragment implements View.OnClickListener {

    public static final String TYPE = "type";    //不同dialog类型

    public int getType() {
        return type;
    }

    private int type = 0;


    private MainActivity context;

    public static final int TYPE_LOADING = 1;
    public static final int TYPE_MERGE = 2;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = (MainActivity) activity;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setContentView(R.layout.dialog_loading_layout);
        initLoadingView(dialog);

        dialog.setCanceledOnTouchOutside(false); // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        final Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);

        return dialog;
    }

    private ImageView iv_loading;

    private TextView tv_loading;


    public static final int CONTINUE = 0;
    public static final int COMPLETE = 1;
    public static final int ERROR = 2;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CONTINUE:
                    tv_loading.append(msg.obj + "\n");
                    if (scrollView != null) {
                        scrollView.post(new Runnable() {
                            public void run() {
                                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                            }
                        });
                    }
                    break;
                case COMPLETE:
                    tv_loading.append(msg.obj + "\n");
                    if (scrollView != null) {
                        scrollView.post(new Runnable() {
                            public void run() {
                                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                            }
                        });
                    }
                    context.getHandler().obtainMessage(MainActivity.MERGE_COMPLETE).sendToTarget();
                    dismiss();
                    break;
                case ERROR:
                    tv_loading.append( msg.obj + "\n");
                    if (scrollView != null) {
                        scrollView.post(new Runnable() {
                            public void run() {
                                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                            }
                        });
                    }
                    context.getHandler().obtainMessage(MainActivity.MERGE_ERRER).sendToTarget();
                    dismiss();
                    break;
            }
        }
    };

    public Handler getHandler() {
        return handler;
    }

    private ScrollView scrollView;
    private void initLoadingView(Dialog dialog) {
        iv_loading = (ImageView) dialog.findViewById(R.id.iv_loading);
        Glide.with(this).asGif().load(R.mipmap.loading).into(iv_loading);

        tv_loading = (TextView) dialog.findViewById(R.id.tv_loading);

        scrollView = (ScrollView) dialog.findViewById(R.id.scrollView);

    }

    @Override
    public void onClick(View view) {

    }
}
